home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / comp / header.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  3.4 KB  |  148 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: header.c,v 1.3 94/06/27 16:49:23 wlott Exp $
  27. *
  28. * This file deals with the file headers.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include <stdio.h>
  33. #include <ctype.h>
  34.  
  35. #include "mindycomp.h"
  36. #include "header.h"
  37. #include "lexer.h"            /* for "line_count" */
  38.  
  39. struct header_handler {
  40.     char *key;
  41.     void (*func)(char *value);
  42.     struct header_handler *next;
  43. };
  44.  
  45. static struct header_handler *handlers = NULL;
  46.  
  47. void add_header_handler(char *key, void (*func)(char *value))
  48. {
  49.     struct header_handler *new = malloc(sizeof(struct header_handler));
  50.  
  51.     new->key = key;
  52.     new->func = func;
  53.     new->next = handlers;
  54.     handlers = new;
  55. }
  56.  
  57. static void process_header(char *key, char *value)
  58. {
  59.     struct header_handler *handler;
  60.  
  61.     for (handler = handlers; handler != NULL; handler = handler->next) {
  62.     if (strcmp(key, handler->key) == 0) {
  63.         (*handler->func)(value);
  64.         return;
  65.     }
  66.     }
  67. }
  68.  
  69. static char *buffer = NULL;
  70. static char *buffer_end = NULL;
  71.  
  72. static char *append(char *fill, int c)
  73. {
  74.     if (fill == buffer_end) {
  75.     int offset = fill - buffer;
  76.     if (buffer) {
  77.         int new_len = (buffer_end - buffer) * 2;
  78.         buffer = realloc(buffer, new_len);
  79.         buffer_end = buffer + new_len;
  80.     }
  81.     else {
  82.         buffer = malloc(1024);
  83.         buffer_end = buffer + 1024;
  84.     }
  85.     fill = buffer + offset;
  86.     }
  87.  
  88.     *fill = c;
  89.     return fill+1;
  90. }
  91.  
  92. static void scan_one_header(FILE *file, int c)
  93. {
  94.     char *fill = buffer;
  95.     char *value;
  96.  
  97.     do {
  98.     if (isupper(c))
  99.         fill = append(fill, tolower(c));
  100.     else
  101.         fill = append(fill, c);
  102.     c = getc(file);
  103.     } while (c != ':' && c != EOF && c != '\n');
  104.     fill = append(fill, '\0');
  105.  
  106.     if (c != ':') {
  107.     fprintf(stderr, "%s:%d: Bogus header: ``%s''\n",
  108.         current_file, line_count, buffer);
  109.     exit(1);
  110.     }
  111.  
  112.     value = fill;
  113.  
  114.     while (1) {
  115.     while ((c = getc(file)) == ' ' || c == '\t')
  116.         ;
  117.     while (c != EOF && c != '\n') {
  118.         fill = append(fill, c);
  119.         c = getc(file);
  120.     }
  121.     if (c == EOF)
  122.         break;
  123.     else
  124.         line_count++;
  125.     c = getc(file);
  126.     if (c == EOF)
  127.         break;
  128.     if (c != ' ' && c != '\t') {
  129.         ungetc(c, file);
  130.         break;
  131.     }
  132.     fill = append(fill, '\n');
  133.     }
  134.     fill = append(fill, '\0');
  135.  
  136.     process_header(buffer, value);
  137. }
  138.  
  139. void read_header(FILE *file)
  140. {
  141.     int c;
  142.  
  143.     while ((c = getc(file)) != EOF && c != '\n')
  144.     scan_one_header(file, c);
  145.     if (c == '\n')
  146.     line_count++;
  147. }
  148.